/*
 * @copyright: Huang Ding
 * @Author: ding-cx
 * @Date: 2022-05-28 15:20:05
 * @LastEditors: ding-cx
 * @LastEditTime: 2022-06-15 16:47:32
 * @Description: 机器相关参数设置
 */

import { actions } from "./actions";
import { hc } from "../hc";
import BaseHttpService from "./BaseHttpService";

export default class ParamsService extends BaseHttpService {
	/**
	 * 设置网络参数 注意如果是设置ip，将重启设备，设备即不会响应结果
	 *
	 * @param {Partial<hc.http.INetParams>} params
	 * @return {*}
	 * @memberof BaseHaiQingInterfaceParamsService
	 */
	async setNetParams(params: Partial<hc.http.INetParams>) {
		const operator = actions.SetNetParam;
		const data = {
			info: {
				IPAddr: params.IPAddr || undefined,
				Submask: params.Submask || undefined,
				WebPort: params.WebPort || undefined,
				ListenPort: params.ListenPort || undefined,
				Gateway: params.Gateway || undefined,
			} as Partial<hc.http.INetParams>,
		};
		await this.request(operator, data);
		return true;
	}

	/**
	 * 获取网络参数配置
	 *
	 * @return {*}
	 * @memberof BaseHaiQingInterfaceParamsService
	 */
	async getNetParams() {
		const operator = actions.GetNetParam;
		return await this.request<hc.http.IResponseGetNetParam>(operator, {});
	}
	/**
	 * 获取设备常用参数配置 3.23.1
	 *
	 * @param {hc.http.machine} machine
	 * @return {*}
	 * @memberof MachineToolsService
	 */
	async getGeneralParam() {
		const operator = actions.GetGeneralParam;
		const data = {};
		const res = await this.request<hc.http.IResponseGetGeneralParam>(operator, data);

		return res;
	}

	/** 设置设备常用参数 */
	async setGeneralParam(params: hc.http.IRequestSetGenneralParam) {
		const operator = actions.SetGeneralParam;
		const info: hc.http.IRequestSetGenneralParam = params;
		const data = { info, DeviceID: this.device.deviceId };
		const res = await this.request<hc.http.IResponseGetGeneralParam>(operator, data);

		return res;
	}

	/** 获取温度配置参数 */
	async getTemperature() {
		const operator = actions.GetTemperature;
		const data = {};
		const res = await this.request<hc.http.IResponseGetTemperature>(operator, data);
		return res;
	}

	/** 设置温度配置参数 */
	async setTemperature(config: Partial<hc.http.ITemperature>) {
		const operator = actions.SetTemperature;
		const data = {
			info: config,
		};
		const res = await this.request<hc.http.response>(operator, data);
		return res;
	}

	/** 获取系统配置参数 */
	async getSysConfig() {
		const operator = actions.GetSysCfg;
		const data = {};
		const res = await this.request<hc.http.IResponseGetSysConfig>(operator, data);
		return res;
	}

	/** 设置系统配置参数 */
	async setSysConfig(config: Partial<hc.http.ISysConfig>) {
		const operator = actions.SetSysCfg;
		const data = {
			info: config,
		};
		const res = await this.request<hc.http.response>(operator, data);
		return res;
	}

	/** 获取人脸配置参数 */
	async getFaceParams() {
		const operator = actions.GetFaceParam;
		const res = await this.request<hc.http.IResponseGetFaceParams>(operator, {});
		return res;
	}

	/** 设置人脸配置参数 */
	async setFaceParams(config: hc.http.IFaceParam) {
		const operator = actions.SetFaceParam;
		const info: hc.http.IFaceParam = config;
		const res = await this.request<hc.http.IResponseGetFaceParams>(operator, { info });
		return res;
	}

	/** 设置http订阅参数 */
	async setSubscribeConfig(config: hc.http.IRequestSubscribeInfo) {
		const operator = actions.Subscribe;
		const info = config;
		const res = await this.request<hc.http.response>(operator, { info });
		return res;
	}

	/** 获取订阅的参数 */
	async getSubscribeConfig() {
		const operator = actions.GetSubscribe;
		const res = await this.request<hc.http.IResponseGetSubscribe>(operator, {});
		return res;
	}

	/** 取消订阅话题 */
	async cancelSubscribe(topics: hc.http.subscribeTopics[]) {
		const operator = actions.Unsubscribe;
		const info: hc.http.IRequestUnsubscribeInfo = {
			DeviceID: this.device.deviceId,
			Num: topics.length,
			Topics: topics,
		};
		const res = await this.request(operator, { info });
		return res;
	}

	/** 获取开门条件参数配置 */
	async getDoorCondition() {
		const operator = actions.GetDoorCondition;
		const data = {};
		const res = await this.request<hc.http.IResponseGetDoorCondition>(operator, data);
		return res;
	}

	/** 设置开门条件参数配置 */
	async setDoorCondition(params: any) {
		const operator = actions.SetDoorCondition;
		const data = {
			info: params,
		};
		const res = await this.request(operator, data);
		return res;
	}

	/** 获取设备音频参数 */
	async getSoundParams() {
		const operator = actions.GetSound;
		const res = await this.request<hc.http.IResponseGetSoundParams>(operator, {});
		return res;
	}

	/** 设置设备音频参数 */
	async setSoundParams(params: any) {
		const operator = actions.SetSound;
		const data: hc.http.IRequestSetSoundParams = {
			operator: operator,
			info: {
				...params,
			},
		};
		const res = await this.request<hc.http.response>(operator, data);
		return res;
	}

	/** 获取设备MQTT参数
	 * @since 2.6.12
	 */
	async getMQTTParams() {
		const operator = actions.GetMQTTParam;
		const res = await this.request<hc.http.IResponseGetMQTTParams>(operator, {});
		return res;
	}

	/** 设置设备MQTT参数
	 * @since 2.6.12
	 *
	 */
	async setMQTTParams(params: hc.http.IRequestSetMQTTParams["info"]) {
		const operator = actions.SetMQTTParam;
		const data: hc.http.IRequestSetMQTTParams = {
			operator: operator,
			info: {
				...params,
			},
		};
		const res = await this.request<hc.http.response>(operator, data);
		return res;
	}
}
